//
//  FXCBottomCollectionView.m
//  Various
//
//  Created by 范新晨 on 16/3/28.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "FXCBottomCollectionView.h"

@interface FXCBottomCollectionView ()
@property (nonatomic, strong) UICollectionViewCell *indexCell;
@end

@implementation FXCBottomCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        self.cellArr = [NSMutableArray array];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    NSLog(@"+++");
    
//    [self.cellArr removeAllObjects];
    
    NSMutableArray *cellArr = [NSMutableArray  array];
    for (UICollectionViewLayoutAttributes *cell in self.visibleCells) {
        
//        NSLog(@"%@", cell);
        
        [cellArr addObject:cell];
        
        
    }
    self.cellArr = cellArr;;
//    NSLog(@"%@", self.cellArr);
    NSArray *sortedArray = [self.cellArr sortedArrayUsingComparator:^(UICollectionViewLayoutAttributes *obj1, UICollectionViewLayoutAttributes *obj2) {
        if (obj1.frame.origin.x < obj2.frame.origin.x) {
            return NSOrderedAscending;
        } else {
            return NSOrderedDescending;
        }
    }];
    self.cellArr = [sortedArray copy];
    
    [self restCellFrame:touches];
   
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    [self restCellFrame:touches];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [UIView animateWithDuration:0.3 delay:0.5 options:UIViewAnimationOptionTransitionNone animations:^{
        
        for (UICollectionViewCell *cell in self.cellArr) {
            if (cell != self.indexCell) {
                cell.y = 50;
            } else {
                cell.y = 15;
            }
        }
        
    } completion:nil];
    //            代理方法
    [self.bottomDelegate bottomCollectionViewChangeCenterView:[self indexPathForCell:self.indexCell]];
}
- (void)restCellFrame:(NSSet<UITouch *> *)touches{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    
    UICollectionViewCell *visCell;
    
    int j;
    for (j = 0; j < self.cellArr.count ; j++) {
        
        visCell = self.cellArr[j];
        
        
        
        
        if (CGRectContainsPoint(CGRectMake(visCell.frame.origin.x, 0, visCell.frame.size.width, visCell.frame.size.height), point)) {
            
            self.indexCell = visCell;

            
            [UIView animateKeyframesWithDuration:0.6 delay:0 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{
                
                [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.2 animations:^{
                    for (int i = 0; i < self.cellArr.count; i++) {
                        UICollectionViewCell *cell = self.cellArr[i];
                        CGFloat gap = abs((j - i)*5);
                        cell.y = 10 + gap;
                    }
                }];
                
            } completion:nil];
        }
    }

}


@end
